package com.evan.review.architecture.mvvm.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.evan.review.R;
import com.evan.review.architecture.mvvm.model.Note;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 笔记适配器
 * 使用ListAdapter，支持DiffUtil进行高效的列表更新
 */
public class NoteAdapter extends ListAdapter<Note, NoteAdapter.NoteViewHolder> {
    
    private OnItemClickListener listener;
    
    // 定义DiffUtil回调，用于比较新旧列表项
    private static final DiffUtil.ItemCallback<Note> DIFF_CALLBACK = new DiffUtil.ItemCallback<Note>() {
        @Override
        public boolean areItemsTheSame(@NonNull Note oldItem, @NonNull Note newItem) {
            return oldItem.getId() == newItem.getId();
        }
        
        @Override
        public boolean areContentsTheSame(@NonNull Note oldItem, @NonNull Note newItem) {
            return oldItem.getTitle().equals(newItem.getTitle()) &&
                    oldItem.getContent().equals(newItem.getContent()) &&
                    oldItem.getCategory().equals(newItem.getCategory()) &&
                    oldItem.getPriority() == newItem.getPriority() &&
                    oldItem.isArchived() == newItem.isArchived() &&
                    oldItem.getUpdatedAt() == newItem.getUpdatedAt();
        }
    };
    
    public NoteAdapter() {
        super(DIFF_CALLBACK);
    }
    
    @NonNull
    @Override
    public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_note_mvvm, parent, false);
        return new NoteViewHolder(itemView);
    }
    
    @Override
    public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) {
        Note currentNote = getItem(position);
        holder.bind(currentNote);
    }
    
    public Note getNoteAt(int position) {
        return getItem(position);
    }
    
    class NoteViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvTitle;
        private final TextView tvContent;
        private final TextView tvCategory;
        private final TextView tvPriority;
        private final TextView tvDate;
        
        public NoteViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_note_title);
            tvContent = itemView.findViewById(R.id.tv_note_content);
            tvCategory = itemView.findViewById(R.id.tv_note_category);
            tvPriority = itemView.findViewById(R.id.tv_note_priority);
            tvDate = itemView.findViewById(R.id.tv_note_date);
            
            // 设置点击事件
            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (listener != null && position != RecyclerView.NO_POSITION) {
                    listener.onItemClick(getItem(position));
                }
            });
        }
        
        public void bind(Note note) {
            tvTitle.setText(note.getTitle());
            tvContent.setText(note.getContent());
            tvCategory.setText(note.getCategory());
            
            // 设置优先级文本和颜色
            String priorityText = "优先级: " + note.getPriority();
            tvPriority.setText(priorityText);
            
            // 根据优先级设置不同的颜色
            int priorityColor;
            switch (note.getPriority()) {
                case 5:
                    priorityColor = itemView.getContext().getResources().getColor(android.R.color.holo_red_dark);
                    break;
                case 4:
                    priorityColor = itemView.getContext().getResources().getColor(android.R.color.holo_red_light);
                    break;
                case 3:
                    priorityColor = itemView.getContext().getResources().getColor(android.R.color.holo_orange_dark);
                    break;
                case 2:
                    priorityColor = itemView.getContext().getResources().getColor(android.R.color.holo_orange_light);
                    break;
                default:
                    priorityColor = itemView.getContext().getResources().getColor(android.R.color.holo_green_dark);
                    break;
            }
            tvPriority.setTextColor(priorityColor);
            
            // 格式化日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
            String formattedDate = sdf.format(new Date(note.getUpdatedAt()));
            tvDate.setText(formattedDate);
        }
    }
    
    public interface OnItemClickListener {
        void onItemClick(Note note);
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}